package com.xinlang.excel.io;


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import com.xinlang.excel.Table;
import com.xinlang.excel.TableUtil;


/**
 * 输出Excel格式的Writer
 * 
 */
public class ExcelTableWriter implements TableWriter {

	// ExcelWriter07 writer = null;
	ExcelWriter writer = null;
	boolean shouldClose = false;

	Object defaultValue = null; // 表示单元为空

	// 按照名字指定Writer
	public ExcelTableWriter(String filename) throws IOException {
		// writer = new ExcelWriter07(filename);
		writer = new ExcelWriter(filename);
		shouldClose = true;
	}

	// 按照名字指定Writer
	public ExcelTableWriter(String filename, Object defaultValue) throws IOException {
		this(filename);
		this.defaultValue = defaultValue;
	}

	// 指定ExcelWriter
	public ExcelTableWriter(ExcelWriter writer) {
		this.writer = writer;
		this.shouldClose = false;
	}

	public ExcelTableWriter(ExcelWriter writer, Object defaultValue) {
		this(writer);
		this.defaultValue = defaultValue;
	}

	public void write(Table table) throws IOException {
		this.write(table, false);
	}

	public void write(Table table, boolean printTitle) throws IOException {
		this.write(table, 0, 0, printTitle);
	}

	public void write(Table table, int rowStart, int columnStart, boolean printTitle)
			throws IOException {
		if (rowStart < 0) {
			rowStart = 0;
		}
		if (columnStart < 0) {
			columnStart = 0;
		}

		String title = table.getTitle() != null ? table.getTitle() : "-";
		writer.createSheet(title);

		try {
			// 自动调整Excel中单元格大小
			// CellView cellView = new CellView();
			// cellView.setAutosize(true);
			// 输出
			int rowNum = rowStart;
			int columnNum = columnStart;

			if (printTitle) {
				int tableWidth = table.getColumnList().size();
				writer.addMergeValue(columnNum, rowNum, tableWidth, rowNum, table.getTitle());
				rowNum++;
			}

			writer.addColorValue(columnNum, rowNum, table.getRowName());

			for (String column : table.getColumnList()) {
				columnNum++;
				writer.addColorValue(columnNum, rowNum, column.toString());
			}
			rowNum++;
			columnNum = 0;
			List<String> rowKeyList = table.getRowKeyList();
			//for (Map.Entry<String, Map<String, Object>> row : table.getRows().entrySet()) {
			for(String key: rowKeyList) {
				Map<String, Object> row = table.getRow(key);
				Method method0 = ExcelWriter.class.getDeclaredMethod("addValue", Integer.class,
						Integer.class, key.getClass());
				method0.invoke(writer, columnNum, rowNum, key);
				// writer.getExcelSheet().autoSizeColumn(columnNum);

				for (String column : table.getColumnList()) {
					columnNum++;
					Object value = row.get(column);
					if (value == null) {
						if (defaultValue == null || defaultValue instanceof String) {
							writer.addValue(columnNum, rowNum, (String) null);
						} else if (defaultValue instanceof Long) {
							writer.addValue(columnNum, rowNum, (Long) defaultValue);
						} else if (defaultValue instanceof Integer) {
							writer.addValue(columnNum, rowNum, (Integer) defaultValue);
						} else if (defaultValue instanceof Double) {
							writer.addValue(columnNum, rowNum, (Double) defaultValue);
						} else {
							writer.addValue(columnNum, rowNum, defaultValue.toString());
						}

					} else {
						Method method;
						// 不考虑公式
						method = ExcelWriter.class.getDeclaredMethod("addValue", Integer.class,
								Integer.class, value.getClass());
						/* } */
						// }
						// method.invoke(writer, new Object[] { columnNum,
						// rowNum, value });
						method.invoke(writer, columnNum, rowNum, value);
						// writer.getExcelSheet().autoSizeColumn(columnNum);
					}
				}
				int picRowNum = 0;
				if (table.getImages() != null && table.getImages().size() != 0) {
					for (BufferedImage image : table.getImages()) {
						if (image == null) {
							continue;
						}
						writer.addPicture(columnNum + 2, picRowNum, writer.getExcelSheet(), image);
						picRowNum += 20;
					}
				}
				rowNum++;
				columnNum = 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new IOException(e);
		}
	}

	@Override
	public void close() {
		if (writer != null && shouldClose) {
			writer.close();
			writer = null;
		}
	}

	static public void main(String[] args) throws IOException {
		ExcelTableWriter writer = new ExcelTableWriter("sample.xls");
		Table table = new Table();
		table.put("3", "C", "3C");
		table.put("2", "C", "2C");
		table.put("2", "B", "2B");
		table.put("1", "A", "1A");
		table.put("10", "X", new ExcelFormula("7/100"));
		table.orderBy("C", false);
		TableUtil.dumpTable(table);
		writer.write(table);
		writer.close();
		System.err.println("done!");
	}

}
